﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

namespace WebApplication10.App_Code
{
    public class Fee
    {
        public int FeeID { get; set; }
        public string Type { get; set; }
        public int Amount { get; set; }
        public string Description { get; set; }

        public static Fee GetFeeByID(string FeeID)
        {
            SqlConnection conn = new SqlConnection();
            Fee f = null;

            try
            {
                conn = new SqlConnection();
                String connstr = ConfigurationManager.ConnectionStrings["BlueSearchDBString"].ToString();
                conn.ConnectionString = connstr;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "select * from Fee where FeeID=@FeeID";
                comm.Parameters.AddWithValue("@FeeID", FeeID);
                SqlDataReader dr = comm.ExecuteReader();
                while (dr.Read())
                {
                    f = new Fee();
                    f.FeeID = (int)dr["FeeID"];
                    f.Type = (string)dr["Type"];
                    f.Amount = (int)dr["Amount"];
                    f.Description = (string)dr["Description"];
                }
                dr.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
            return f;
        }

        public static List<Fee> GetFee()
        {
            List<Fee> results = new List<Fee>();
            SqlConnection conn = null;

            try
            {
                conn = new SqlConnection();
                string connstr = ConfigurationManager.ConnectionStrings["BlueSearchDBString"].ToString();
                conn.ConnectionString = connstr;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "Select * from Fee";
                SqlDataReader dr = comm.ExecuteReader();

                while (dr.Read())
                {
                    Fee f = new Fee();
                    f = new Fee();
                    f.FeeID = (int)dr["FeeID"];
                    f.Type = (string)dr["Type"];
                    f.Amount = (int)dr["Amount"];
                    f.Description = (string)dr["Description"];
                }
                dr.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
            return results;
        }

        public static void UpdateFee(Fee f)
        {
            SqlConnection conn = new SqlConnection();

            try
            {
                conn = new SqlConnection();
                string connstr = ConfigurationManager.ConnectionStrings["BlueSearchDBString"].ToString();
                conn.ConnectionString = connstr;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText =
                    "Update Fee SET Type=@Type, Amount=@Amount, Description=@Description WHERE FeeID=@FeeID";
                comm.Parameters.AddWithValue("@Type", f.Type);
                comm.Parameters.AddWithValue("@Amount", f.Amount);
                comm.Parameters.AddWithValue("@Descriptio", f.Description);
                int rowsAffected = comm.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }

        public static void DeleteFee(Fee f)
        {
            SqlConnection conn = new SqlConnection();

            try
            {
                conn = new SqlConnection();
                string connstr = ConfigurationManager.ConnectionStrings["BlueSearchDBString"].ToString();
                conn.ConnectionString = connstr;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText =
                    "Delete from Fee Where FeeID=@FeeID";
                comm.Parameters.AddWithValue("@FeeID", f.FeeID);
                int rowsAffected = comm.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }

        public static int InsertFee(Fee f)
        {
            SqlConnection conn = null;
            int newFeeID = 0;

            try
            {
                string connstr = ConfigurationManager.ConnectionStrings["BlueSearchDBString"].ToString();
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "Insert into Fee (Type, Amount, Description) VALUE (@Type, @Amount, @Description)";
                comm.Parameters.AddWithValue("@Type", f.Type);
                comm.Parameters.AddWithValue("@Amount", f.Amount);
                comm.Parameters.AddWithValue("@Descriptio", f.Description);
                int rowsAffected = comm.ExecuteNonQuery();
                comm.CommandText = "selected @@IDENTITY as newFeeID";
                SqlDataReader dr = comm.ExecuteReader();
                dr.Read();
                newFeeID = int.Parse(dr["NewFeeID"].ToString());
                dr.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
            return newFeeID;
        }
    }
}